package com.visionblue.business.ship.modbus

import android.util.Log
import com.deemons.serialportlib.SerialPort
import com.visionblue.business.ship.model.bean.local.OilFlowInfo
import com.visionblue.business.ship.model.bean.local.UltraSonicFlowInfo
import com.visionblue.business.ship.model.bean.local.WaterFlowInfo
import com.visionblue.business.ship.model.bean.local.WeightInfo
import java.io.File

/**
 * @author: xlh
 * @date: 2020-05-29 5:12 PM
 * @desc:
 */
object DeviceControllerManager {

    //---------------serial  port info --------------
    private const val UART = 3
    private const val BAUD_RATE = 9600
    private const val PARITY = 0
    private const val DATA_BITS = 8
    private const val STOP_BIT = 1

    // --------------slave  id ---------------

    //输入生活污水
    private const val INPUT_WATER_SLAVE_ID = 2
    //输出生活污水
    private const val OUTPUT_WATER_SLAVE_ID = 3

    //输入新增-含油污水地址
    private const val INPUT_WATER_SLAVE_ID_22 = 22
    //输出新增-含油污水
    private const val OUTPUT_WATER_SLAVE_ID_33 = 33
    //输入废机油地址
    private const val INPUT_OIL_SLAVE_ID = 4
    //输出废机油地址
    private const val OUTPUT_OIL_SLAVE_ID = 5

    //地磅地址
    private const val WEIGHT_ID = 6
    //输入声波地址
    private const val INPUT_SONIC_SLAVE_ID = 7
    //输出声波地址
    private const val OUTPUT_SONIC_SLAVE_ID = 8

    private val oilInfoParser = OilInfoParser()
    private val waterInfoParser = WaterInfoParser()
    private val sonicInfoParser = UltraSonicInfoParser()

    private val modBusMaster =
        ModBusMaster(SerialPort(File("/dev/ttyS$UART"), BAUD_RATE, PARITY, DATA_BITS, STOP_BIT, 0))

//    private val weightModBusMaster = ModBusMaster(SerialPort(File("/dev/ttyS$UART"), BAUD_RATE, PARITY, DATA_BITS, STOP_BIT, 0))

    // 废机油
    fun readInputOilFlowInfo(): OilFlowInfo {
        return try {
//            val results = modBusMaster.readInputRegisters(INPUT_OIL_SLAVE_ID, 0x000D, 8) - 2021-04-9 -wh
            val results = modBusMaster.readHoldingRegisters(INPUT_OIL_SLAVE_ID, 0x000D, 8)
            oilInfoParser.parse(results)
        } catch (e: Exception) {
            OilFlowInfo()
        }
    }

    fun readOutOilFlowInfo(): OilFlowInfo {
        return try {
//            val results = modBusMaster.readInputRegisters(OUTPUT_OIL_SLAVE_ID, 0x000D, 8) - 2021-04-9 -wh
            val results = modBusMaster.readHoldingRegisters(OUTPUT_OIL_SLAVE_ID, 0x000D, 8)
            oilInfoParser.parse(results)
        } catch (e: Exception) {
            OilFlowInfo()
        }
    }

    // 生活污水
    fun readInputWaterFlowInfo(): WaterFlowInfo {
        return try {
            // 读取生活污水通道读数，寄存器起始地址1010，长度22个-一次读取所有寄存器数据
          val results = modBusMaster.readInputRegisters(INPUT_WATER_SLAVE_ID, 0x1010, 22)
         waterInfoParser.parse(results)
        } catch (e: Exception) {
            WaterFlowInfo()
        }

    }

    fun readOutputWaterFlowInfo(): WaterFlowInfo {
        return try {
           val results = modBusMaster.readInputRegisters(OUTPUT_WATER_SLAVE_ID, 0x1010, 22)

            waterInfoParser.parse(results)
        } catch (e: Exception) {
            WaterFlowInfo()
        }
    }

    //新增-含油污水-start
    fun readInputWasteWaterFlowInfo(): WaterFlowInfo {
        return try {
            val results = modBusMaster.readInputRegisters(INPUT_WATER_SLAVE_ID_22, 0x1010, 22)
            Log.e("流量计-wasterWater",results.toString())
            waterInfoParser.parse(results)
        } catch (e: Exception) {
            WaterFlowInfo()
        }
    }

    fun readOutputWasteWaterFlowInfo(): WaterFlowInfo {
        return try {
            val results = modBusMaster.readInputRegisters(OUTPUT_WATER_SLAVE_ID_33, 0x1010, 22)
            waterInfoParser.parse(results)
        } catch (e: Exception) {
            WaterFlowInfo()
        }
    }

    //新增-含油污水-end


    fun readWeightInfo(): WeightInfo {

       var result = modBusMaster.readInputRegister(WEIGHT_ID, 0)
        //var result  = 80
        if(result>2000||result<0){
           // Log.e("------>weight1",result.toString())
            result = 0
           // Log.e("------>weight2",result.toString())
        }
       // Log.e("------>weight3",result.toString())

        return WeightInfo().apply {
            weight = result
        }

    }

    fun readInputSonicFlowInfo(): UltraSonicFlowInfo {
        return try {
            val results = modBusMaster.readHoldingRegisters(INPUT_SONIC_SLAVE_ID, 0, 12)//正式
//            val factor = modBusMaster.readHoldingRegisters(INPUT_SONIC_SLAVE_ID, 1437, 2)
            sonicInfoParser.parse(results)
        } catch (e: Exception) {
            UltraSonicFlowInfo()
        }

    }

    fun readOutputSonicFlowInfo(): UltraSonicFlowInfo {
        val results = modBusMaster.readHoldingRegisters(OUTPUT_SONIC_SLAVE_ID, 0, 12)
//        val factor = modBusMaster.readHoldingRegisters(OUTPUT_SONIC_SLAVE_ID, 1437, 2)
        return sonicInfoParser.parse(results)
    }

}